<?php
class SiteAdmin_LocationController extends SiteAdminController
{

   	public function indexAction()
    {
		$country_id = $this->getRequest()->getParam('country', false);
		$countries = array();
		$_countries = Obj_Country::getCountries();
		$countries[0] = _('Выберите страну ...');
		foreach ($_countries as $v) {
			$countries[$v['ID']] = $v['Name'];
		}
		$this->view->countries = $countries;
		$region_id = (int)$this->getRequest()->getParam('region', 0);
		$this->view->region = (int)$region_id;
		$region = Obj_Region::getRegion($region_id);
    	if ($country_id === false && $region != 0) $country_id = $region['CountryID'];
    	$this->view->country = (int) $country_id;
		$regions = array();
		$_regions = Obj_Region::getRegions($country_id);
    	$regions[0] = _('Выберите регион ...');
		foreach ($_regions as $v) {
			$regions[$v['ID']] = $v['Name'];
		}
		$this->view->regions = $regions;

		if(($region_id != 0)){
			$country_id = $region['CountryID'];
			$region_id = $this->getRequest()->getParam('region', 0);
		}
		$_cities = Obj_City::getCities($region['ID']);
		$cities = array();
		$cities[0] = _('Выберите город ...');
		foreach ($_cities as $v) {
			$cities[$v['ID']] = $v['Name'];
		}
		$this->view->cities = $cities;
		$city_id = (int)$this->getRequest()->getParam('city', 0);
    	$city = Obj_City::getCity($city_id);
    	if(($country_id === false) && ($region_id == 0) && ($city_id != 0)){
			$country_id = $city['CountryID'];
			$region_id = $city['RegionID'];			
			$this->view->countries = $countries;
	    	$this->view->country = $city['CountryID'];	    	
	    	$_regions = Obj_Region::getRegions($city['CountryID']);
	    	$regions[0] = _('Выберите регион ...');
			foreach ($_regions as $v) {
				$regions[$v['ID']] = $v['Name'];
			}	    	
			$this->view->regions = $regions;
			$this->view->region = $city['RegionID'];			
	    	$_cities = Obj_City::getCities($city['RegionID']);
			$cities = array();
			$cities[0] = _('Выберите город ...');
			foreach ($_cities as $v) {
				$cities[$v['ID']] = $v['Name'];
			}			
	    	$this->view->cities = $cities;
    		$this->view->city = (int)$city_id;    	
    	}
		$this->view->locations = Obj_Location::getLocations($city_id);
    }

   	public function editAction()
    {
    	$id = (int) $this->getRequest()->getParam('id', 0);
    	$addnew = intval($this->getRequest()->getParam('addnew', 0)) === 1 ? true : false;
    	$arLocation = Obj_Location::getLocation($id);
    	if ($addnew || $arLocation === false) {
    		$this->view->city = array(
    			'ID' => 0,
    			'CountryID' => 0,
    			'RegionID' => 0,
    			'CityID' => 0,
    			'Name' => '',
    			'alias' => '',
    			'active' => 0,
    			'seoTitle' => '',
    			'seoDescription' => '',
    			'seoKeywords' => '',
    		);
    	}
    	else {
    		$this->view->location = $arLocation;
    	}    	
    	$ar = Obj_Country::getCountries();
    	$countries = array();
    	foreach ($ar as $v) {
    		$countries[$v['ID']] = $v['Name'];
    	}
    	$this->view->countries = $countries;
    	$ar = Obj_City::getCities(reset(array_keys($countries)));
    	$cities = array();
    	foreach ($ar as $v) {
    		$cities[$v['ID']] = $v['Name'];
    	}
    	$this->view->regions = $cities;
    }
    
    public function reloadRegionsAction() {
    	if (!$this->_isAJAX) {
    		throw new Zend_Exception(_('Эта функция Вам недоступна'));
    	}
    	$id = (int) $this->getRequest()->getParam('id', 0);
    	$arRegions = Obj_Region::getRegions($id);
    	$regions = array();
    	if ($arRegions !== false) foreach ($arRegions as $v) $regions[$v['ID']] = $v['Name'];
    	$this->_helper->json(array('error' => 0, 'regions' => $regions), true, false);
    }
    
    public function reloadCitiesAction() {
    	if (!$this->_isAJAX) {
    		throw new Zend_Exception(_('Эта функция Вам недоступна'));
    	}
    	$id = (int) $this->getRequest()->getParam('id', 0);
    	$arCities = Obj_City::getCities($id);
    	$cities = array();
    	if ($arCities !== false) foreach ($arCities as $v) $cities[$v['ID']] = $v['Name'];
    	$this->_helper->json(array('error' => 0, 'cities' => $cities), true, false);
    }
    	
    public function saveAction() {
    	if (!$this->_isAJAX) {
    		throw new Zend_Exception(_('Эта функция Вам недоступна'));
    	}
    	//Data validation
    	$arLocation = array();
    	$id = (int) $this->getRequest()->getParam('ID', 0);
    	$arLocation['Name'] = trim($this->getRequest()->getParam('Name', ''));
    	if ($arLocation['Name'] == '') {
    		$this->_helper->json(array('error' => 1, 'errorStr' => _('Город должен иметь название --- !')), true, false);
    	}
    	
    	$arLocation['CountryID'] = intval($this->getRequest()->getParam('CountryID', ''));
    	if ($arLocation['CountryID'] === 0) {
    		$this->_helper->json(array('error' => 1, 'errorStr' => _('Город должен иметь страну!')), true, false);
    	}
    	$arLocation['RegionID'] = intval($this->getRequest()->getParam('RegionID', ''));
    	if ($arLocation['RegionID'] === 0) {
    		$this->_helper->json(array('error' => 1, 'errorStr' => _('Город должен иметь регион!')), true, false);
    	}
    	$arLocation['CityID'] = intval($this->getRequest()->getParam('CityID', ''));
    	if ($arLocation['CityID'] === 0) {
    		$this->_helper->json(array('error' => 1, 'errorStr' => _('Город должен иметь регион!')), true, false);
    	}
    	$arLocation['alias'] = Obj_Text::translit($arLocation['Name']);
    	if ($arLocation['alias'] == '') {
    		$this->_helper->json(array('error' => 1, 'errorStr' => _('Город должен иметь алиас!')), true, false);
    	}
    	$arLocation['seoTitle'] = trim($this->getRequest()->getParam('seoTitle', ''));
    	$arLocation['seoDescription'] = trim($this->getRequest()->getParam('seoDescription', ''));
    	$arLocation['seoKeywords'] = trim($this->getRequest()->getParam('seoKeywords', ''));
    	$arLocation['active'] = intval($this->getRequest()->getParam('active', 0));
		//Add data into DB
    	$cid = Obj_Location::saveLocation($arLocation, $id);
    	if ($arLocation['active'] === 1) Obj_System_SearchIndex::write($cid, CONTENT_TYPE_LOCATION, $arLocation['Name']);
    	else Obj_System_SearchIndex::delete($cid, CONTENT_TYPE_LOCATION);     	
    	$this->_helper->json(array('error' => 0, 'redirect' => $this->view->url(array('action' => 'index', 'city' => $arLocation['CityID']))), true, false);
    }
}